package com.myprocessing.controller;

import org.springframework.context.ApplicationContext;

import com.myprocessing.box.Box;
import com.myprocessing.gui.PGuiFrameApplet;
import com.myprocessing.gui.ScreenManager;
import com.myprocessing.gui.util.Font;
import com.myprocessing.gui.util.ProcessingObjectWrapper;
import com.myprocessing.timer.Timer;
import com.myprocessing.timer.TimerManager;

public class GuiController {
	protected static PGuiFrameApplet pGuiFrameApplet;
	protected static ScreenManager screenManager;
	protected static TimerManager timerManager;
	protected static ApplicationContext uiCtx;

	public PGuiFrameApplet getPGuiFrameApplet() {
		return pGuiFrameApplet;
	}

	public static void setPGuiFrameApplet(PGuiFrameApplet guiFrameApplet) {
		pGuiFrameApplet = guiFrameApplet;
	}

	public ScreenManager getScreenManager() {
		return screenManager;
	}

	public void setScreenManager(ScreenManager screenManager) {
		this.screenManager = screenManager;
	}
	
	public static ApplicationContext getUiCtx() {
		return uiCtx;
	}

	public static void setUiCtx(ApplicationContext uiCtx) {
		GuiController.uiCtx = uiCtx;
	}

	public static void init(PGuiFrameApplet pGuiFrameApplet, ApplicationContext uiCtx){
		setPGuiFrameApplet(pGuiFrameApplet);
		setUiCtx(uiCtx);
		
		ScreenManager.setPApplet(pGuiFrameApplet);

        ProcessingObjectWrapper.setPApplet(pGuiFrameApplet);
        Font.setPApplet(pGuiFrameApplet);
        Box.setPApplet(pGuiFrameApplet);
		  
        for(String str: uiCtx.getBeanDefinitionNames()){
        	if(str.endsWith("Screen")){
        		screenManager.getScreenMap().put(str, (Box)uiCtx.getBean(str));
        	} else if(str.endsWith("Timer")){
        		timerManager.getTimers().add((Timer)uiCtx.getBean(str));
        	} else if(!str.endsWith("Base")){
        		Object o = uiCtx.getBean(str);
        		if(o instanceof Box){
        			Box box = (Box)o;
        			if(box.getParent() != null){
        				box.getParent().addSubBox(box);
        			}
        		}
        	}
        }
        
        for(String str: uiCtx.getBeanDefinitionNames()){
        	if(str.endsWith("Timer")){
        		timerManager.getTimers().add((Timer)uiCtx.getBean(str));
        	}
        }

        
		if (timerManager != null) {
			timerManager.setPApplet(pGuiFrameApplet);
			timerManager.init();
		}
		pGuiFrameApplet.initP();
		
		screenManager.init();
	}
	
	
	public void onMousePress() {
		screenManager.onMousePress();
	}
	
	public void draw() {
		executeCurrentState();
		screenManager.display();
		screenManager.onMouseOver();
		screenManager.onMouseEnter();
		screenManager.onMouseExit();
		screenManager.onNotMouseOver();
		if (timerManager != null) {
			timerManager.update(pGuiFrameApplet.millis());
		}
	}
	
	public void executeCurrentState(){}

	public TimerManager getTimerManager() {
		return timerManager;
	}

	public void setTimerManager(TimerManager timerManager) {
		GuiController.timerManager = timerManager;
	}
	

	public void onkeyPressed(char key) {
		screenManager.onkeyPressed(key);
	}
}
